﻿using Avalonia;
using Avalonia.Controls;
using Avalonia.Data.Converters;

namespace Digitalmes.AvaloniaUI.Converters;

/// <summary>
/// Icon 转换器，通过资源文件找到对应的 Icon。
/// </summary>
/// <remarks>
/// Avalonia UI Fluent Icons 提供了一系列 icon 集合，可参考：<see cref="https://avaloniaui.github.io/icons.html"/>。
/// </remarks>
public sealed class IconFromResourceConverter : IValueConverter
{
    public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        if (value is string s && Application.Current!.TryFindResource(s, out var result) && result is not null)
        {
            return result;
        }

        return AvaloniaProperty.UnsetValue;
    }

    public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        return AvaloniaProperty.UnsetValue;
    }
}
